package com.pd.backend.service.logistics.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pd.backend.common.PageResult;
import com.pd.backend.common.R;
import com.pd.backend.common.utils.StringUtils;
import com.pd.backend.common.utils.page.PageUtils;
import com.pd.backend.domain.dto.FleetDto;
import com.pd.backend.domain.entity.PdAuthUser;
import com.pd.backend.domain.pojo.Fleet;
import com.pd.backend.domain.pojo.Truck;
import com.pd.backend.domain.pojo.TruckDriver;
import com.pd.backend.ex.CustomerException;
import com.pd.backend.mapper.AuthUserMapper;
import com.pd.backend.mapper.FleetMapper;
import com.pd.backend.mapper.TruckDriverMapper;
import com.pd.backend.mapper.TruckMapper;
import com.pd.backend.service.logistics.TruckFleetService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;


/**
 * 车队业务层实现类
 */
@Service
@Slf4j
public class TruckFleetServiceImpl implements TruckFleetService {

    @Autowired
    private FleetMapper fleetMapper;

    @Autowired
    private TruckMapper truckMapper;

    @Autowired
    private TruckDriverMapper truckDriverMapper;

    @Autowired
    private AuthUserMapper authUserMapper;

    /**
     * 车队-多条件分页查询
     *
     * @param size        每页数据条数
     * @param current     当前页
     * @param manager     负责人id（用户id）
     * @param fleetNumber 车队编号
     * @param name        车队名称
     * @return
     */
    @Override
    public PageResult<Fleet> getPage(Integer size, Integer current, String manager, String fleetNumber, String name) {
        // 处理参数
        if (size == null || size <= 0) {
            size = 10;
        }
        if (current == null || current <= 0) {
            current = 1;
        }
        if (StringUtils.isNotEmpty(fleetNumber)) {
            fleetNumber = "%" + fleetNumber + "%";
        }
        if (StringUtils.isNotEmpty(name)) {
            name = "%" + name + "%";
        }
        // 开始分页
        Page<Fleet> page = PageHelper.startPage(current, size);
        List<Fleet> list = fleetMapper.getSearch(manager, fleetNumber, name);

        return PageUtils.copyPage(page);
    }

    /**
     * 根据车队id分页查询车辆信息
     *
     * @param size    每页数据条数
     * @param current 当前页
     * @param fleetId 车队id
     * @return
     */
    @Override
    public PageResult<Truck> getTruckList(Integer size, Integer current, String fleetId) {
        // 处理参数
        if (size == null || size <= 0) {
            size = 10;
        }
        if (current == null || current <= 0) {
            current = 1;
        }
        Page<Truck> page = PageHelper.startPage(current, size);
        List<Truck> list = truckMapper.getTruckListByFleetId(fleetId);
        return PageUtils.copyPage(page);
    }

    /**
     * 根据车队id分页查询司机信息
     *
     * @param size    每页数据条数
     * @param current 当前页
     * @param fleetId 车队id
     * @return
     */
    @Override
    public PageResult<TruckDriver> getDriverList(Integer size, Integer current, String fleetId) {
        // 处理参数
        if (size == null || size <= 0) {
            size = 10;
        }
        if (current == null || current <= 0) {
            current = 1;
        }
        Page<TruckDriver> page = PageHelper.startPage(current, size);
        List<TruckDriver> list = truckDriverMapper.getDriverListByFleetId(fleetId);
        return PageUtils.copyPage(page);
    }


    /**
     * 修改车队
     *
     * @param fleetDto
     * @return
     */
    @Override
    @Transactional
    public R modify(FleetDto fleetDto) {
        // 处理参数
        String id = fleetDto.getId();
        String name = fleetDto.getName();
        String userId = fleetDto.getUserId();
        String mobile = fleetDto.getMobile();

        if (StringUtils.isEmpty(id)) {
            throw new CustomerException("参数异常");
        }
        if (StringUtils.isEmpty(name)) {
            throw new CustomerException("参数异常");
        }
        if (StringUtils.isEmpty(userId)) {
            throw new CustomerException("参数异常");
        }
        if (StringUtils.isEmpty(mobile)) {
            throw new CustomerException("参数异常");
        }
        // 查询用户表是否有该用户
        PdAuthUser user = authUserMapper.findById(userId);
        if (Objects.isNull(user)){
            throw new CustomerException("数据库无该用户");
        }
        // 修改车队信息
        int i = fleetMapper.modify(id, name, userId);
        // 通过userId修改手机号
        int j = authUserMapper.modifyMobileByUserId(userId, mobile);

        return null;
    }
}
